package qjc.h_二叉树;

import com.二叉树.练习题.TreeNode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class h1_二叉树的中序遍历 {
    /**
     * 一路向左，将所有左子节点入栈，弹出栈顶节点，转向右子树，重复上述过程
     *
     * @param root
     * @return
     */
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> queue = new LinkedList<>();
        TreeNode cur = root;
        while (cur != null || !queue.isEmpty()) {
            while (cur != null) {
                queue.push(cur);
                cur = cur.left;
            }
            cur = queue.pop();
            res.add(cur.val);
            cur = cur.right;
        }
        return res;
    }
}
